
template.defaults.imports.date_format_iso=function(str){
    return date_format_iso(str);
}
template.defaults.imports.dateFormat=function(str){
    return data_format(str);
}
template.defaults.imports.date_format_time=function(str){
    return data_format_time(str);
}
template.defaults.imports.log=console.log
template.defaults.imports.log=console.log
template.defaults.imports.eval=eval

template.defaults.imports.gettext=gettext

//function data_format(str)
//{
//    return str.substr(0,10) + "_" + str.substr(11,8);
//}

function date_format_iso(str)
{
    str =String(str)
    return str.substr(0,10) + " " + str.substr(11,8);
}

function data_format(str)
{
    str =String(str)
    return str.substr(5,5) + " " + str.substr(11,5);
}

function data_format_time(str)
{
    str =String(str)
    return str.substr(11,8);
}

function floor_number(value, scale) {
    var sca=Math.pow(10, scale);
    var val=Calculate.accMul(value, sca);
    val=Math.floor(val);
    return val/sca;
}
template.defaults.imports.floor_number=floor_number

function fmoney(s, n)
{
   n = n > 0 && n <= 20 ? n : 2;
   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
   var l = s.split(".")[0].split("").reverse(),
   r = s.split(".")[1];
   t = "";
   for(i = 0; i < l.length; i ++ )
   {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
   }
   return t.split("").reverse().join("") + "." + r;
}

function rmoney(s)
{
   return parseFloat(s.replace(/[^\d\.-]/g, ""));
}

var coin_tofixed_type_price = 'price_floor'
var coin_tofixed_type_count = 'count_floor'
var coin_tofixed_type_amount = 'amount_floor'
var coin_tofixed_type_depth_amount = 'depth_amount_floor'

template.defaults.imports.coin_tofixed=coin_tofixed
function coin_tofixed(coin_type, use_coin_type, tofixed_type)
{
    var skey = coin_type + '2' + use_coin_type
    var info = g_markets[skey]
//    console.log("get coin_name tofixed " + coin_name )
    if (!info)
    {
        //console.log("coin_name " + coin_name + " userinfo not exist ")
        return 0
    }
    return info[tofixed_type];
}

function get_fa_icon(coin_type)
{
    if (coin_type == 'bitcny') {
        return "fa fa-cny"
    }
    if (coin_type == 'usdt') {
        return "fa fa-usd"
    }
    if (coin_type == 'usd') {
        return "fa fa-usd"
    }
    if (coin_type == 'btc') {
        return "fa fa-btc"
    }
    return ""
}

template.defaults.imports.get_coin_icon=get_coin_icon
function get_coin_icon(coin_name, img)
{
//    console.log("coin_name " + coin_name )
    var info = g_coin_info[coin_name]
    var str = ""
    var fa_str = get_fa_icon(coin_name)
    if (fa_str.length > 0) {
        str = "<i class=\"" + fa_str + "\"></i>";
    //    console.log("fafa ===================== " + fa_str )
    } else if (info.icon_svg.length > 0) {
        //str = "<img class=\"svg \"  src=\"" + info.icon_svg +"\"/>"
        str = "<img class=\"svg \"  src=\"" + info.icon_svg +"\"/>"
    } else {
        str = "<img class=\"svg \"  src=\"" + info.icon_img +"\"/>"
    }
    //console.log("get_coin_icon-------------- " + coin_name + " return : " + str )
    return str;
}

template.defaults.imports.get_coin_icon_url=get_coin_icon_url
function get_coin_icon_url(coin_name, img)
{
//    console.log("coin_name " + coin_name )
    var info = g_coin_info[coin_name]
    var str = ""
    if (info.icon_svg.length > 0) {
        //str = "<img class=\"svg \"  src=\"" + info.icon_svg +"\"/>"
        str = info.icon_svg
    } else {
        str = info.icon_img
    }
    //console.log("get_coin_icon-------------- " + coin_name + " return : " + str )
    return str;
}

template.defaults.imports.get_coin_info=get_coin_info
function get_coin_info(coin_name)
{
    console.log("get coin info coin_name " + coin_name )
    var info = g_coin_info[coin_name]
    console.log("get coin info coin_name " + info )
    return info;
}
function init_inlink_svg() {
    jQuery('img.svg').each(function(){
        var $img = jQuery(this);
        var imgID = $img.attr('id');
        var imgClass = $img.attr('class');
        var imgURL = $img.attr('src');

        jQuery.get(imgURL, function(data) {
            // Get the SVG tag, ignore the rest
            var $svg = jQuery(data).find('svg');

            // Add replaced image's ID to the new SVG
            if(typeof imgID !== 'undefined') {
                $svg = $svg.attr('id', imgID);
            }
            // Add replaced image's classes to the new SVG
            if(typeof imgClass !== 'undefined') {
                $svg = $svg.attr('class', imgClass+' replaced-svg');
            }

            // Remove any invalid XML tags as per http://validator.w3.org
            $svg = $svg.removeAttr('xmlns:a');

            // Replace image with new SVG
            $img.replaceWith($svg);

        }, 'xml');
    });
};

/**
 * 计算
 * */
var Calculate = (function () {
    return {
        //过滤输入的数字
        checkNumberByName_new: function (name,length){
            var number = document.getElementById(name).value.split('.');
            if(number.length > 1){
                return number[0].replace(/\D/g, '') + '.' + number[1].replace(/\D/g, '').slice(0, length);
            }else{
                return number[0].replace(/\D/g,'');
            }
        },
        //加法
        accAdd: function(arg1,arg2){
            var r1,r2,m;
            try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;}
            try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;}
            m=Math.pow(10,Math.max(r1,r2));
            return (arg1*m+arg2*m)/m;
        },
        //乘法
        accMul: function(arg1,arg2) {
            var m=0,s1=arg1.toString(),s2=arg2.toString();
            try{m+=s1.split(".")[1].length;}catch(e){}
            try{m+=s2.split(".")[1].length;}catch(e){}
            return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
        },
        //除法
        accDiv: function(arg1,arg2){
            var t1=0,t2=0,r1,r2;
            try{t1=arg1.toString().split(".")[1].length;}catch(e){}
            try{t2=arg2.toString().split(".")[1].length;}catch(e){}
            with(Math){
                r1=Number(arg1.toString().replace(".",""));
                r2=Number(arg2.toString().replace(".",""));
                return (r1/r2)*pow(10,t2-t1);
            }
        },
        //加法
        accAdd_z: function(arg1,arg2){
            return (Number(arg1)+Number(arg2)).toFixed(8);
        },
        //乘法
        accMul_z: function(arg1,arg2) {
            return (Number(arg1)*Number(arg2)).toFixed(8);
        },
        //除法
        accDiv_z: function(arg1,arg2) {
            if(Number(arg2)==0){
                return 0;
            }
            return (Number(arg1)/Number(arg2)).toFixed(8);
        },
        // 向下截位 number截位位数
        DownTruncation: function (val, number) {
            return this.CommaFormatted(this.floor(val, number), number);
        },
        CommaFormattedCommon: function(s, n) {
            n = n > 0 && n <= 20 ? n : 2;
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";

            return s;
        },
        CommaFormattedLittle: function(s, n) {
            if(Number(s)<1000){
                return Number(s).toFixed(n);
            }
            return (Number(s).toFixed(n) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
        },
        CommaFormatted: function(s, n) {
            var k = n;
            n = n > 0 && n <= 20 ? n : 2;
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
            var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
            t = "";
            for (var i = 0; i < l.length; i++) {
                t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
            }
            var result =t.split("").reverse().join("") + "." + r;
            if(Number(s)<0){
                if(result!=null&&result.substr(0,2)=="-,"){
                    result=result.substr(2,result.length);
                    result="-"+result;
                }
            }
            if(k==0){
                return result.split(".")[0];
            }
            return result;
        },
        //只千分位，不补位，0.00-->0
        CommaFormattedByOriginal: function(s) {
            if (s) {
                var temp = s.split(".");
                var tempLength = temp.length==2?temp[1].length:0;
                return this.CommaFormatted(s,tempLength);
            }
            return 0;
        },

        //只千分位，不补位，0.00-->0
        CommaFormattedOnly: function(s) {
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")) + "";
            var slength = s.split(".").length;
            var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
            t = "";
            for (var i = 0; i < l.length; i++) {
                t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
            }
            var result;
            if(slength>1){
                result =t.split("").reverse().join("") + "." + r;
            }else{
                result =t.split("").reverse().join("");
            }
            if(Number(s)<0){
                if(result!=null&&result.substr(0,2)=="-,"){
                    result=result.substr(2,result.length);
                    result="-"+result;
                }
            }
            return result;
        },
        /**
         *
         * @param data数据
         * @param symbol币种0-btc,1-ltc,2-eth,4-etc
         * @returns {*}
         */
        formatTickerData: function(data,symbol) {
            data.vol = CommaFormattedByOriginal(data.vol + "");
            data.last = Number(data.last).toFixed(symbolSubPaltPoint(symbol));
            return data;
        },
        /**
         * 保留小数位
         * value：值，rate：截位（四舍五入）
         */
        formatNumber: function(value,rate){
            var result=CommaFormatted(value,rate);
            if(rate==0||!rate){
                if(!!result&&result.indexOf(".")!=-1){
                    return result.substring(0, result.length-3);
                }
                return result ;
            }
            return result;
        },
        /**
         * 截取指定位数
         * @param value
         * @param rate
         * @returns {*}
         */
        formatNumberTruncate: function (value, rate) {
            return formatValue(floor(value, rate), rate);
        },
        /**
         * 向上整数
         */
        round: function(value,scale){
            var sca=Math.pow(10, scale);
            var val=value*sca;
            val=Math.ceil(val);
            return val/sca;
        },
        /**
         * 向下取证
         */
        floor: function(value,scale){
            var sca=Math.pow(10, scale);
            var val=this.accMul(value,sca);
            val=Math.floor(val);
            return val/sca;
        }
    }
})();
template.defaults.imports.Calculate=Calculate

template.defaults.imports.sort_by=sort_by
function sort_by(attr,rev){
    //第二个参数没有传递 默认升序排列
    if(rev ==  undefined){
        rev = 1;
    }else{
        rev = (rev) ? 1 : -1;
    }
    return function(a,b){
        a = a[attr];
        b = b[attr];
        if(a < b){
            return rev * -1;
        }
        if(a > b){
            return rev * 1;
        }
        return 0;
    }
}

// 原来结构也会改
template.defaults.imports.sort_by_weight=sort_by_weight
function sort_by_weight(data, wkey, wdata)
{
    //console.log(data)
    //console.log(wdata)
    var newdata = JSON.parse(JSON.stringify(data));

    for (var i = 0; i < newdata.length; i ++)
    {
        var coin_type = newdata[i].coin_type
        newdata[i][wkey] = wdata[coin_type][wkey]
    }
    newdata.sort(sort_by(wkey, false))
    //console.log(data)
    //console.log(newdata)
    return newdata
}
